<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <h3>add:{{ add() }}</h3>
    <h3>add:{{ add() }}</h3>

    <h3>sum:{{sum}}</h3>
    <h3>sum:{{sum}}</h3>

    x <input type="text" v-model.number="data.x">
    y <input type="text" v-model.number="data.y">
  </div>

  <script type="module">
    import { createApp, reactive, computed } from '../vue.esm-browser.js'
    createApp({
      setup() {
        const data = reactive({
          x: 10,
          y: 20
        })
        // 方法-无缓存 相当于调用两次，影响性能
        let add = () => {
          console.log('add');
          return data.x + data.y
        }
        //计算属性-有缓存，当数据没有变化时，多次调用只返回一次，当数据变化时可以响应式数据变化
        const sum = computed(() => {
          console.log('sum');
          return data.x + data.y
        })
        return {
          data,
          sum,
          add
        }
      }
    }).mount('#app')
  </script>
</body>

</html>